package com.quick.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
@Configuration
public class QuickLocaleResolver implements LocaleResolver {
    @Autowired
    private HttpServletRequest request;
    public Locale getLocal() {
        return resolveLocale(request);
    }
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String language = request.getHeader("Accept-Language");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(language)){
            //zh_CN
            String[] s = language.split("-");
            //国家，地区
            locale = new Locale(s[0], s[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
